/
YearMonths.php
85 lines (72 loc) 路 1.82 KB
/
YearMonths.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace Aeon\Calendar\Gregorian;
use Aeon\Calendar\Exception\InvalidArgumentException;
/**
* @psalm-immutable
*/
final class YearMonths implements \Countable
{
private Year $year;
public function __construct(Year $year)
{
$this->year = $year;
}
public function count() : int
{
return $this->year->numberOfMonths();
}
public function byNumber(int $number) : Month
{
return new Month($this->year, $number);
}
/**
* @return array<int, Month>
*/
public function all() : array
{
/** @psalm-suppress ImpureFunctionCall */
return \array_map(
fn (int $monthNumber) : Month => new Month($this->year, $monthNumber),
\range(1, $this->year->numberOfMonths())
);
}
/**
* @psalm-param pure-callable(Month $day) : void $iterator
*
* @param callable(Month $day) : void $iterator
*
* @return array<mixed>
*/
public function map(callable $iterator) : array
{
return \array_map(
$iterator,
$this->all()
);
}
/**
* @psalm-param pure-callable(Month $day) : bool $iterator
*
* @param callable(Month $day) : bool $iterator
*
* @return array<Month>
*/
public function filter(callable $iterator) : array
{
return \array_filter(
$this->all(),
$iterator
);
}
public function slice(int $from, int $size) : Months
{
if ($from < 0) {
throw new InvalidArgumentException('Slice out of range.');
}
if ($from + $size > 12) {
throw new InvalidArgumentException('Slice out of range.');
}
return Months::fromArray(...\array_slice($this->all(), $from, $size));
}
}